
#ifndef  TRANSAPPENDER_H
#define  TRANSAPPENDER_H

#include <memory>
#include <string>
#include "ITransaction.h"
#include "ILog.h"


class Topic;

class TransAppender {
public:
    TransAppender(Topic & topic);
	~TransAppender();
    bool open();
    bool close();

    class Transaction : public ITransaction {
    public:
        Transaction(std::unique_ptr<ILog> & log);
        ~Transaction() override;

        Transaction & execute(long long key, const std::string & value);
        recovery::Status commit() override;
        recovery::Status rollback() override;
    private:
        std::unique_ptr<ILog> & _log;
        size_t _lastTransOffset = 0; // last transaction offset in log
        size_t _size = 0; // appendBytes for this trans

        static const int TRANS_STATUS_READY = 0;
        static const int TRANS_STATUS_EXECUTING = 1;
        int _status = TRANS_STATUS_READY;
    };

	std::unique_ptr<Transaction> transaction();


private:
	std::unique_ptr<ILog> _log;
	/*  
    class Batch : public ITransaction {
    public:
        Batch & write(const std::string & data);
        recovery::Status commit() override;
        recovery::Status rollback() override;
    };
    Batch & batch() 
    */
};

#endif   /* TRANSAPPENDER_H */
